/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.fatecmm.cadastrotimes.servlets;

import br.edu.fatecmm.cadastrotimes.banco.PaisDAO;
import br.edu.fatecmm.cadastrotimes.beans.BnPais;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author raphael
 */
public class SltPais extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        operacao = request.getParameter("operacao");
        chave = request.getParameter("chave");

        RequestDispatcher rd = null;
        
        if("incluir".equals(operacao))
        {
            rd = request.getRequestDispatcher("/incluirPais.jsp");
        }
        else if("gravarinclusao".equals(operacao))
        {
            bnPais = new BnPais();
            bnPais.setDs_pais(request.getParameter("ds_pais"));
            
            try
            {
                paisDAO = new PaisDAO();
                paisDAO.salvar(bnPais);

                rd = request.getRequestDispatcher("SltPais?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("editar".equals(operacao))
        {
            try
            {
                bnPais = new BnPais();
                paisDAO = new PaisDAO();
                bnPais = paisDAO.procurar(Integer.parseInt(chave));
                HttpSession session = request.getSession(true);
                session.setAttribute("bnPais", bnPais);
                rd = request.getRequestDispatcher("/alterarPais.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("gravaralteracao".equals(operacao))
        {
            bnPais = new BnPais();
            bnPais.setId_pais(Integer.parseInt(request.getParameter("id_pais")));
            bnPais.setDs_pais(request.getParameter("ds_pais"));
            
            
            try
            {
                paisDAO = new PaisDAO();
                paisDAO.atualizar(bnPais);

                rd = request.getRequestDispatcher("SltPais?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("listar".equals(operacao))
        {
            try
            {
                paisDAO = new PaisDAO();
                List itens = paisDAO.todos();
                request.setAttribute("itens", itens);
                rd = request.getRequestDispatcher("/paisListar.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("listar_pais".equals(operacao))
        {
            try
            {
                paisDAO = new PaisDAO();
                List itens = paisDAO.todos();
                request.setAttribute("itens", itens);
                rd = request.getRequestDispatcher("/paisListar.jsp"); 
            }
             catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("excluir".equals(operacao))
        {
            try
            {
                bnPais = new BnPais();
                paisDAO = new PaisDAO();
                bnPais.setId_pais(Integer.parseInt(chave));
                paisDAO.excluir(bnPais);
                rd = request.getRequestDispatcher("StlPais?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        

        if(rd != null)
            rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    private String operacao;
    private String chave;
    private BnPais bnPais;
    private PaisDAO paisDAO;
}
